﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Adams.Admin.DataAccess.Console
{
    public static class Profile
    {
        public static Adams.Admin.DataTransfer.Console.Profile.PageLoad PageLoad(int userId)
        {
            var data = new Adams.Admin.DataTransfer.Console.Profile.PageLoad();
            data.User = new Adams.Shared.Entities.Console.User();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT *
FROM Console.Users
WHERE UserID = @UserID
", CommandType.Text,
            DB.CreateParameter("@UserID", userId, SqlDbType.Int)))
            {
                dr.Read();
                data.User.ID = dr["UserID"].DbInt();
                data.User.FirstName = dr["FirstName"].DbString();
                data.User.LastName = dr["LastName"].DbString();
                data.User.Email = dr["Email"].DbString();
                data.User.IsActive = dr["IsActive"].DbBool();
                data.User.IsLocked = dr["IsLocked"].DbBool();
            }

            return data;
        }

        public static void SaveProfile(DataTransfer.Console.Profile.FormInput input)
        {
            DB.Run(@"
UPDATE Console.Users SET
    FirstName = @FirstName, 
    LastName = @LastName,
    Email = @Email
WHERE UserID = @UserID
    ", CommandType.Text,
                    DB.CreateParameter("@UserID", input.ID.ToInt()),
                    DB.CreateParameter("@FirstName", input.FirstName.ToNString()),
                    DB.CreateParameter("@LastName", input.LastName.ToNString()),
                    DB.CreateParameter("@Email", input.Email.ToNString()));
        }

//        public static Adams.Admin.DataTransfer.Console.Profile.UserInfo GetUserInfo(string email)
//        {
//            var data = new Adams.Admin.DataTransfer.Security.Login.UserInfo();

//            using (SqlDataReader dr = DB.RunDataReader(@"
//SELECT *
//FROM Console.Users
//WHERE Email = @Email
//", CommandType.Text,
//                DB.CreateParameter("@Email", email, SqlDbType.VarChar, 50)))
//            {
//                if (dr.HasRows)
//                {
//                    dr.Read();
//                    data.UserID = dr["UserID"].DbInt();
//                    data.PasswordHash = dr["PasswordHash"].DbString();
//                    data.PasswordSalt = dr["PasswordSalt"].DbString();
//                    data.IsActive = dr["IsActive"].DbBool();
//                    data.LastLoginAttempt = dr["LastLoginAttempt"].DbDate();
//                    data.FailedLoginAttempts = dr["FailedLoginAttempts"].DbInt();
//                    data.Exists = true;
//                }
//                else
//                {
//                    data.Exists = false;
//                }
//            }

//            return data;
//        }
    }
}
